#!/usr/bin/python2.6
#coding:utf8

import web
import os

PHOTO_DIR = '/root/rest/rest-python-sever/static/flicker'

MONITOR_URL = "http://10.37.10.123:9090/photo/monitor/"
STATIC_URL = "http://10.37.10.123:9090/static/flicker/"

import time
import minjson

class MyUpload:
        def POST(self):
                global MONITOR_URL
                response = {}
                x = web.input(photo={},name="test.jpg")
                web.header('Content-Type','application/json')
                if 'photo' in x:
                        try:
                                filename = x['name']
                                time_suffix = time.strftime("%H%M%S")
                                filename = "%s_%s" % (time_suffix,filename)
                                target = os.path.join(PHOTO_DIR,filename)
                                fout = open(target,'w')
                                fout.write(x.photo.file.read())
                                fout.close()
                        except Exception,e:
                                response['name'] = x['name']
                                response['status'] = 1
                                response['err_msg'] = repr(e)
                                response['monitor_url'] = MONITOR_URL
                        else:
                                response['status'] = 0
                                response['path_on_server'] = target
                                response['monitor_url'] = MONITOR_URL
                                response['picture_url'] = "%s%s" % (STATIC_URL,filename)
                        return getattr(minjson,"write")(response)
                else:
                         response['status'] = 1
                         response['err_msg'] = "The file not in body"
                         response['monitor_url'] = MONITOR_URL
                return getattr(minjson,"write")(response)


class RestUpload:
        def POST(self):
                web.header("Content-Type","application/json")
                data = web.data()
                response = {}
                try:
                        time_suffix = time.strftime("%H%M%S")
                        filename = "%s_%s" % (time_suffix,"rest_test.jpg")
                        target = os.path.join(PHOTO_DIR,filename)
                        fout = open(target,'w')
                        fout.write(data)
                        fout.close()
                except Exception,e:
                        response['status'] = 1
                        response['err_msg'] = repr(e)
                        response['monitor_url'] = MONITOR_URL
                else:
                        response['status'] = 0
                        response['path_on_server'] = target
                        response['monitor_url'] = MONITOR_URL
                        response['picture_url'] = "%s%s" % (STATIC_URL,filename)

                return getattr(minjson,"write")(response)

class UploadForm:
        def GET(self):
                web.header("Content-Type","text/html;charset=utf-8")
                return """
                        <html><head></head><body>
                        <form method="POST" enctype="multipart/form-data" action="/photo/upload/">
                        <input type="file" name="photo" />
                        <br/>
                        <input type="submit" />
                        </form>
                        </body></html>
                        """

class Monitor:
        def GET(self):
                PATH = PHOTO_DIR
                files = os.listdir(PATH)
                index = 1
                response = []
                for file in files:
                        img = {}
                        img['id'] = index
                        index+=1
                        img['url'] = "%s%s" % (STATIC_URL,file)
                        response.append(img)
                web.header('Content-Type','application/json')
                return getattr(minjson,"write")(response)
